<link rel="stylesheet" href="__STATIC_LIB__layuiadmin/layui/layui_ext/dtree/dtree.css">
<link rel="stylesheet" href="__STATIC_LIB__layuiadmin/layui/layui_ext/dtree/font/dtreefont.css">

<style>
    @media screen and (max-width: 500px) {
        
        .layui-layer.layui-layer-page {
            width: 100% !important;
            overflow-x: scroll !important;
            left: 0 !important;
        }

        .layui-layer-title {
            width: 100% !important;
            box-sizing: border-box;
        }

        .layui-layer-content {
            width: 100% !important;
        }
    }
</style>
<form class="layui-form seller-form" lay-filter="userForm" id="userForm" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">手机号码：</label>
            <div class="layui-input-inline">
                <input type="text" id="initstatus" name="initstatus" value = "0" autocomplete="off"
                    class="layui-input layui-hide">
                <input type="text" id="pid" name="pid" value = "1" autocomplete="off"
                    class="layui-input layui-hide">
                <input type="text" id="pname" name="pname" value = "黄河科技学院" autocomplete="off"
                    class="layui-input layui-hide">
                <input type="text" id="mobile" name="mobile" lay-verify="title" placeholder="请输入手机号码" autocomplete="off"
                    class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">姓名：</label>
            <div class="layui-input-inline">
                <input type="text" id="truename" name="truename" placeholder="请输入姓名" autocomplete="off"
                    class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="*"><i
                    class="iconfont icon-chaxun"></i>筛选</button>
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="user-add"><i
                    class="layui-icon">&#xe608;</i>添加</button> 
            <button id="exportBtn" class="layui-btn layui-btn-sm layui-btn-disabled" lay-submit lay-filter="export-user"><i
                    class="iconfont icon-clouddownload" style="font-size: 20px !important;" disabled></i>导出</button> 
            <button type="button" class="layui-btn layui-btn-sm" lay-submit lay-filter="import-user"><i
                    class="iconfont icon-cloudupload" style="font-size: 20px !important;" disabled></i>导入</button>
        </div>
        <div class="layui-inline">
        </div>
        <div class="layui-inline">
        </div>
    </div>
</form>

<div class="table-body">
    <div class="layui-row layui-col-space10" style="margin-left: 0;">
        <div id="treeContainer" class="layui-col-md3" style="overflow: auto; margin: 15px 0;border-width: 1px; border-style: solid; border-color: #e6e6e6;">
            <tree id="deptTree" class="dtree" data-id="0"></tree>
        </div>
        <div id="tableContainer" class="layui-col-md9">
            <table id="userTable" lay-filter="userTable"></table>
        </div>
    </div>
</div> 

<div id="exportUser" style="display: none;">
    <form class="layui-form export-form" action="">
        <div class="layui-form-item">
            <div class="layui-margin-10">
                <blockquote class="layui-elem-quote layui-text">
                    请先筛选要导出的用户，默认导出全部
                </blockquote>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">任务名称：</label>
                <input type="text" name="taskname" lay-verify="title" style="width:200px;" placeholder="请输入任务名称"
                    autocomplete="off" class="layui-input">
            </div>
        </div>
    </form>
</div>


<div id="importUser" style="display: none;">
    <form class="layui-form import-form" method="post" action="{:url('ietask/import')}" enctype="multipart/form-data">
        <div class="layui-form-item">
            <div class="layui-margin-10">
                <blockquote class="layui-elem-quote layui-text">
                    请先下载<a href="{:url('ietask/importTemplete',['tplName'=>'user'])}">导入模板(不支持迅雷等下载工具)</a>
                </blockquote>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">
                    <input type="file" class="layui-file" name="importFile" id="importFile">
                    <input type="hidden" name="model" value="User">
                </label>
            </div>
        </div>
    </form>
</div>

<script src="__STATIC_JS__jquery.form.js" type="text/javascript" charset="utf-8"></script>
<script>
    layui.extend({
        dtree: '__STATIC_LIB__layuiadmin/layui/layui_ext/dtree/dtree'
    }).use(['form', 'layedit', 'laydate', 'dtree', 'table'], function () { 
        var layer = layui.layer, table = layui.table, form = layui.form, dtree = layui.dtree; 

        userTable = layui.table.render({
            elem: '#userTable',
            height: 'full-115',
            cellMinWidth: '80',
            page: 'true',
            limit: '20',
            url: "{:url('user/index')}?_ajax=1&pid=1",
            id: 'userTable',
            cols: [[
                { type: 'numbers' },
                { field: 'mobile', title: '手机号码', width: 120 },
                { field: 'deptName', title: '部门名称', width: 150 },
                { field: 'positionName', title: '职位', width: 80 },
                { field: 'truename', title: '姓名', width: 80 },
                { field: 'nickname', title: '昵称', width: 120 }, 
                {
                    field: 'avatar', title: '头像',
                    templet: function (d) {
                        if (d.avatar) {
                            return '<a href="javascript:void(0);" onclick=viewImage("' + d.avatar + '")><image style="max-width:30px;max-height:30px;" src="' + d.avatar + '"/></a>';
                        } else {
                            return '<a href="javascript:void(0);" onclick=viewImage("<?php echo config('zlserver.default_image'); ?>")><image style="max-width:30px;max-height:30px;" src="<?php echo config('zlserver.default_image');?>"/></a>';
                        }
                    }, width: 80
                }, 
                { field: 'status', title: '状态', width: 60 },
                {
                    field: 'option', title: '操作', templet: function (d) {
                        var html = '<a class="layui-btn layui-btn-xs edit" data-id="' + d.id + '">修改</a>';
                        html += '<a class="layui-btn layui-btn-xs unbind" data-id="' + d.id + '">清除绑定</a>'; 
                        return html;
                    }, align: 'center', width: 160
                }
            ]],
            done: function(res, curr, count){
                // 判断加载结果数量是否大于0
                if(count>0){
                    $("#exportBtn").removeClass("layui-btn-disabled");
                    $('#exportBtn').removeAttr("disabled");
                } else {
                    $('#exportBtn').addClass("layui-btn-disabled");
                    $('#exportBtn').attr("disabled", true);
                }
            }
        });

        tree = dtree.render({
            elem: "#deptTree",
            initLevel: 2,
            request:{"id":1},
            url: '{:url("Dept/getList")}',
            checkbar: false,    
            cache: true,
            checkbarType: "all",
            response:{message:"msg",statusCode:0},
            dataStyle: "layuiStyle",
            done: function(res, $ul, first){
                dtree.dataInit("deptTree", $('#pid').val()); 
                if($("#tableContainer").height() > 0) {
                    $("#initstatus").val('1'); 
                }
                $("#treeContainer").height($("#tableContainer").height() - 32);
            }
        });
  
        $(window).resize(function() {
            $("#treeContainer").height($("#tableContainer").height() - 32);
        });　

        // 绑定节点点击
        dtree.on("node('deptTree')" ,function(obj){
            // 清理搜索条件
            $('#mobile').val("");
            $('#truename').val("");
            $('#pid').val(obj.param.nodeId);
            $('#pname').val(obj.param.context);

            table.reload('userTable', {
                where: {pid: obj.param.nodeId, pname: obj.param.pname, mobile: '', truename: ''}
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        //搜索
        layui.form.on('submit(*)', function (data) {
            table.reload('userTable', {
                where: data.field
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            
            // 如果有筛选条件则
            // 重置选中节点为根节点 
            if($('#mobile').val() != '' || 
                $('#truename').val() != '') {
                dtree.dataInit("deptTree", '1'); 
                $('#pid').val('1');
                $('#pname').val('黄河科技学院');
            }

            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
 
        //添加
        layui.form.on('submit(user-add)', function () { 
            var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
            topLayui.index.openTabsPage("{:url('User/addUser')}?pid="+$('#pid').val(), '添加用户'); 
            return false;
        });

        //编辑
        $(document).on('click', '.edit', function () { 
            var user_id = $(this).attr('data-id');
            var url = "{:url('manage/User/editUser')}" + '?user_id=' + user_id;
            var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
            topLayui.index.openTabsPage(url, '编辑用户'); 
            return false;
        });
 
        //清除绑定
        $(document).on('click', '.unbind', function () { 
            var user_id = $(this).attr('data-id');

            $.ajax({
                type: "POST",
                url: "{:url('manage/User/unbind')}",
                data: "user_id=" + user_id,
                success: function(data) {
                    layer.msg(data.msg);
                }
            });
            return false;
        });

        //用户导出
        layui.form.on('submit(export-user)', function (data) {
            layer.open({
                type: 1,
                title: '用户导出',
                area: ['400px', '248px'], //宽高
                btn: ['确定', '取消'],
                content: $("#exportUser").html(),
                success: function () {
                    $(':focus').blur();
                    this.enterEsc = function(event){
                        if(event.keyCode === 13){
                            if(!$(".layui-layer-btn0").hasClass("layui-btn-disabled")){
                                $(".layui-layer-btn0").addClass("layui-btn-disabled");

                                //判断是否有选中
                                var filter = $(".seller-form").serialize(); 
                                $(".export-form:last").append("<input type='hidden' name='filter' value='" + filter + "'>");
                                var data = $(".export-form:last").serializeArray();
                                
                                if($.trim(data[0]['value']) == '') {
                                    layer.msg('请填写任务名称！', { time: 1300 });
                                    $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                    return false;
                                }

                                data.push({ 'name': 'model', 'value': 'User' });
                                JsPost("{:url('Ietask/export')}", data, function (res) {
                                        layer.msg(res.msg, { time: 1500 }, function () {
                                            if (res.status) {
                                                layer.closeAll();
                                                //执行跳转
                                                var topLayui = parent === self ? layui : top.layui;
                                                topLayui.index.openTabsPage('/manage/ietask/index.html', '导入导出');
                                            } else {
                                                $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                            }
                                        });
                                    }
                                );
                            }
                            return false; //阻止系统默认回车事件
                        }
                    };
                    $(document).on('keydown', this.enterEsc);	//监听键盘事件，关闭层
                },
                yes: function () {
                    if(!$(".layui-layer-btn0").hasClass("layui-btn-disabled")){
                        $(".layui-layer-btn0").addClass("layui-btn-disabled");

                        //判断是否有选中
                        var filter = $(".seller-form").serialize(); 
                        $(".export-form:last").append("<input type='hidden' name='filter' value='" + filter + "'>");
                        var data = $(".export-form:last").serializeArray();
                        
                        if($.trim(data[0]['value']) == '') {
                            layer.msg('请填写任务名称！', { time: 1300 });
                            $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                            return false;
                        }

                        data.push({ 'name': 'model', 'value': 'User' });
                        JsPost("{:url('Ietask/export')}", data, function (res) {
                                layer.msg(res.msg, { time: 1500 }, function () {
                                    if (res.status) {
                                        layer.closeAll();
                                        //执行跳转
                                        var topLayui = parent === self ? layui : top.layui;
                                        topLayui.index.openTabsPage('/manage/ietask/index.html', '导入导出');
                                    } else {
                                        $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                    }
                                });
                            }
                        );
                    }
                }, btn2: function () {
                    layer.closeAll();
                }, end: function(){
                    $(document).off('keydown', this.enterEsc);	        //解除键盘关闭事件
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //用户导入
        layui.form.on('submit(import-user)', function (data) {
            var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
            topLayui.index.openTabsPage('{:url("User/importUser")}', '导入用户'); 
            return false;
        });
    });
</script>